package com.yict.csms.resourceplan.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.csms.resourceplan.dao.IRtgcFlPlanAllDao;
import com.yict.csms.resourceplan.entity.RtgcFlContractor;
import com.yict.csms.resourceplan.entity.RtgcFlPlan;
import com.yict.csms.resourceplan.entity.RtgcFlPlanAll;

@Repository
public class RtgcFlPlanAllDaoImpl extends CommonDao<RtgcFlPlanAll, Long>
		implements IRtgcFlPlanAllDao {

	public boolean saveOrUpdateRtgcFlPlan(List<RtgcFlPlan> rtgcFlPlan) throws Exception {
		boolean bool = false;
		try {
			this.getTemplate().saveOrUpdateAll(rtgcFlPlan);
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}

	public boolean saveOrUpdateRtgcFlContractor(List<RtgcFlContractor> rtgcFlContractor)
			throws Exception {
		boolean bool = false;
		try {
			this.getTemplate().saveOrUpdateAll(rtgcFlContractor);
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}

	public RtgcFlPlanAll findRtgcFlPlanAllById(Long id){
		RtgcFlPlanAll vo = new RtgcFlPlanAll();
		try {
			vo = (RtgcFlPlanAll)this.getTemplate().get(RtgcFlPlanAll.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	public RtgcFlPlan findRtgcFlPlanById(Long id){
		RtgcFlPlan vo = new RtgcFlPlan();
		try {
			vo = (RtgcFlPlan)this.getTemplate().get(RtgcFlPlan.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	public boolean delRtgcFlPlan(Set<RtgcFlPlan> list) throws Exception {
		boolean bool = false;
		try {
			this.getTemplate().deleteAll(list);
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	public RtgcFlContractor findContractorById(Long id){
		RtgcFlContractor vo = new RtgcFlContractor();
		try {
			vo = (RtgcFlContractor)this.getTemplate().get(RtgcFlContractor.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	
	public boolean delRtgcFlContractor(Set<RtgcFlContractor> list) throws Exception {
		boolean bool = false;
		try {
			this.getTemplate().deleteAll(list);
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	public List<RtgcFlContractor> findByCompanyId(String queryString, Map<String, Object> valueMap) {
		List<RtgcFlContractor> list = new ArrayList<RtgcFlContractor>();
		try {
			if (queryString != null && !"".equals(queryString)) {
				Query query = this.getSession().createQuery(queryString);
				if (query != null && valueMap != null && valueMap.size() > 0) {
					Set<Map.Entry<String, Object>> entrys = valueMap.entrySet();
					for (Map.Entry<String, Object> entry : entrys) {
						String key = entry.getKey();
						Object value = entry.getValue();
						if (value instanceof Object[] && value != null) {
							Object[] os = (Object[]) value;
							query.setParameterList(key, os);
						} else if (value instanceof Collection && value != null) {
							Collection<Object> os = (Collection<Object>) value;
							query.setParameterList(key, os);
						} else if (value != null && !"".equals(value)) {
							query.setParameter(key, value);
						}

					}
				}
				list = query.list();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
